<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

          <a-col :md="8" :sm="24">
            <a-form-item label="出库单号">
              <a-input allowClear v-model="queryParam.orderNumber" placeholder="请输入出库单号"/>
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="24">
            <a-form-item label="类型">
              <a-select 
                allowClear
                placeholder="请选择" 
                style="width: 272px"
                v-model="queryParam.noOrderOrCreateOrder"
              >
                <a-select-option value="1">无单出库</a-select-option>
                <a-select-option value="2">生产出库</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
           

          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <!--<a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>-->
            </span>
          </a-col>

          <!-- <a-col :md="24" :sm="24">
            <a-form-item label="供应商：">
              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdSupplier">
                <a-radio-button value="">
                  全部
                </a-radio-button>
                <a-radio-button :value="id" v-for="{id, name} in suppliers">
                  {{ name }}
                </a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col> -->

          <!-- <a-col :md="24" :sm="24"   v-if="canReadOrgIdCompany()">
            <a-form-item label="归属公司：">
              <a-radio-group default-value="" button-style="solid" v-model="queryParam.orgIdCompany">
                <a-radio-button value="">
                  全部
                </a-radio-button>
                <a-radio-button :value="id" v-for="{id, name} in orgs">
                  {{ name }}
                </a-radio-button>
              </a-radio-group>

            </a-form-item>
          </a-col> -->

        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      :columns="columns"
      :data="loadData"
      :alert="true"
      :rowKey="(record) => record.code"
      :rowSelection="null"
    >
      <span slot="type" slot-scope="text, record">
        {{text?"生产出库":"无单出库"}}
      </span>
      <span slot="sourceOrderNumber" slot-scope="text, record">
        {{text?text:"——"}}
      </span>
      <span slot="action" slot-scope="text, record">
        <a @click="$refs.detailForm.show(record)">明细</a>
      </span>
    </s-table>

    <detail-form ref="detailForm" :material-type="materialType"/>

  </a-card>
</template>

<script>
import {STable} from '@/components'
import {page} from '@/api/modular/main/emsorder/emsOrderManage'

import detailForm from './detailForm'

export default {
  components: {
    STable,
    detailForm,
  },
  data() {
    return {
      advanced: false, // 高级搜索 展开/关闭
      queryParam: {},  // 查询参数
      time:'',
      columns: [
        {
          title: '序号',
          width: '80px',
          customRender: (text, record, index) => `${index + 1}`
        },
        // sourceOrderNumber
        {
          title: '出库单号',
          dataIndex: 'orderNumber'
        },
        {
          title: '出库时间',
          dataIndex: 'createTime'
        },
        {
          title: '类型',
          dataIndex: 'sourceOrderNumber',
          scopedSlots: {customRender: 'type'}
        },
        {
          title: '生产单号',
          dataIndex: 'sourceOrderNumber',
          scopedSlots: {customRender: 'sourceOrderNumber'}
        },
        
        {
          title: '操作',
          dataIndex: 'action',
          width: '250px',
          scopedSlots: {customRender: 'action'}
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.orderType = this.$businessEnum.ORDER_TYPE_ENUM.outOrder.id   //订单类型，获得的是出库单id
        this.queryParam.materialType = this.materialType
        if(this.$route.params.year && this.$route.params.mounth){
          this.queryParam.time = this.$route.params.year + '-' + this.$route.params.mounth
        }else{
          this.queryParam.time = ''
        }
        return page(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      scheduleProductionVisible: false,
      selectedRowKeys: [],
      selectedRows: [],
      defaultExpandedKeys: [],
      replaceFields: {
        key: 'id',
      },
      urlParam: {},
      columnsType: '',
      suppliers: [],
      orgs: [],
    }
  },
  computed: {
    routePath() {
      let urlParam = this.$util.getUrlParam(this.$route.path)
      return urlParam
    },
    materialType() {
      return this.routePath.materialType
    },
  },
  watch: {
    '$route.path': function (newValue) {
      setTimeout(() => {
        this.handleOk()
        this.$refs.table.clearSelected()
      }, 100)
    }
  },
  created() {
    this.suppliers = this.$util.getOrgList([this.$businessEnum.ORG_TYPE_ENUM.PRIMARY_SUPPLIER.id, this.$businessEnum.ORG_TYPE_ENUM.NORMAL_SUPPLIER.id])
    this.orgs = this.$util.getOrgList([this.$businessEnum.ORG_TYPE_ENUM.COMPANY.id])
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    canReadOrgIdCompany() {
      return !this.hasRole(this.$util.ROLE_ENUM.GS_ADMIN.id) && !this.hasRole(this.$util.ROLE_ENUM.MATERIAL_ADMIN3.id)
    },
  }
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}

button {
  margin-right: 8px;
}
</style>
